﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<div class="clearfix" style="padding: 3px 0px;">
    <div class="floatl" style="width: 80px; text-align: right; margin-right: 10px;">
        领导：
    </div>
    <div class="floatl tokenizer text w300">
        <div id="selected-teachers">
            <!-- <a class="token" href="javascript:void(0);">要砂厅<span class="close" title="移除"></span> -->
        </div>
        <input type="text" id="tbname" class="text w200" value="" title="请输入领导名称..." />
        <input type="hidden" id="LeaderIds" name="LeaderIds" />
        <input type="hidden" id="LeaderNames" name="Leader" />
    </div>
</div>
<script type="text/javascript">
    $(function () {
        teacherIds = [];
        teacherNames = [];
        function addTea(id, name) {
            var h = '<a class="token" href="javascript:void(0);">' + name + '<span class="close" title="移除"></span></a>';
            $(h).find("span.close").click(function () {
                $(this).parent().remove();
                var tmp = [];
                var i = 0;
                for (; i < teacherIds.length; i++) {
                    if (id !== teacherIds[i]) {
                        tmp.push(teacherIds[i]);
                    }
                }
                teacherIds = tmp;
            }).end().appendTo("#selected-teachers");
        }


        var ids = $("#LeaderIds").val().split(",");

        var i = 1;
        for (; i < ids.length; i++) {
            if (ids[i - 1].length != 0) {
                teacherIds.push(ids[i - 1]);
                addTea(ids[i - 1], names[i - 1]);
            }
        }



        var acTeacher = cy.ui.autoComplete({
            eleId: 'tbname',
            itemClicked: function (item) {
                var i = 0;
                var exists = false;
                for (; i < teacherIds.length; i++) {
                    if (item.id === teacherIds[i]) {
                        exists = true;
                        break;
                    }
                }
                if (exists) {
                    return false;
                }
                teacherIds.push(item.id);
                teacherNames.push(item.name);
                addTea(item.id, item.name);
            },
            itemFocused: function (item) {
            },
            itemUnfocused: function (item) {
            },
            valueChanged: function (text) {
                if (text.length < 1) {
                    return;
                }
                $.ajax({
                    url: '<%= Url.Action("SearchLeader","Home") %>',
                    data: { s: text },
                    dataType: "json",
                    type: "get",
                    success: function (data, textStatus) {
                        if (data.success && data.list !== null) {
                            if (data.list) {
                                acTeacher.setItems(data.list);
                            } else {
                                acTeacher.setItems([]);
                            }
                        }
                    }
                });
            },
            initText: '请输入领导姓名...',
            maxHeight: 168 // 21的倍数
        });

        $("#mainForm").submit(function () {
            $("#LeaderIds").val(teacherIds.join(','));
            $("#LeaderNames").val(teacherNames.join(','));
        });
    });
</script>
